package com.liuqi.openai.data.segment;

import com.liuqi.openai.data.document.Metadata;

import static com.liuqi.openai.util.ValidationUtil.ensureNotBlank;
import static com.liuqi.openai.util.ValidationUtil.ensureNotNull;

/**
 * 文本的片段
 *
 * @author liuqi
 * @date 2025/7/19
 **/
public class TextSegment {

    /**
     * 片段内容
     */
    private final String text;

    /**
     * 元数据信息
     */
    private final Metadata metadata;

    public TextSegment(String text, Metadata metadata) {
        this.text = ensureNotBlank(text, "text");
        this.metadata = ensureNotNull(metadata, "metadata");
    }

    public String text() {
        return text;
    }

    public Metadata metadata() {
        return metadata;
    }

    public String metadata(String key) {
        return metadata.get(key);
    }

    public static TextSegment from(String text) {
        return new TextSegment(text, new Metadata());
    }

    public static TextSegment from(String text, Metadata metadata) {
        return new TextSegment(text, metadata);
    }

    public static TextSegment textSegment(String text) {
        return from(text);
    }

    public static TextSegment textSegment(String text, Metadata metadata) {
        return from(text, metadata);
    }

    @Override
    public String toString() {
        return "TextSegment {" +
                " text = " + text +
                " metadata = " + metadata.asMap() +
                " }";
    }

}
